export function storageSync() {
  return new Promise(() => {
    chrome.storage.sync()
  })
}


// 推送通知
export function pushNotification(title, msg) {
  if (Notification.permission == 'granted') {
    var notification = new Notification(title, {
      body: msg,
      // icon:图片地址,
    })
    notification.onclick = function () {
      notification.close()
    }
  }
}

// 打开新窗口
export function newTab(url) {
  chrome.tabs.create({
    url: url,
  })
}

// chrome.storage 存储数据
export function setStorage(key, value) {
  chrome.storage.local.set({
    [key]: value,
  })
}

// chrome.storage 获取数据
export function getStorage(key) {
  return new Promise((resolve, reject) => {
    chrome.storage.local.get(key, function (result) {
      resolve(result[key])
    })
  })
}


async function openDB() {
  return new Promise((resolve, reject) => {
    const request = indexedDB.open('mhg-db', 1);
    request.onerror = function (event) {
      console.log('数据库打开报错');
    };
    request.onsuccess = function (event) {
      console.log('数据库打开成功');
      resolve(request.result);
    };
  });

}

export async function getAllBookmarks() {
  const db = await openDB();
  const transaction = db.transaction(['bookmarks'], 'readonly');
  const store = transaction.objectStore('bookmarks');
  const request = store.getAll();

  return new Promise((resolve, reject) => {
    request.onsuccess = function (event) {
      resolve(event.target.result);
    };
    request.onerror = function (event) {
      reject(event.target.error);
    };
  });
}

export async function addBookmark(bookmark) {
  const db = await openDB();
  const transaction = db.transaction(['bookmarks'], 'readwrite');
  const store = transaction.objectStore('bookmarks');
  const request = store.add(bookmark);
  return new Promise((resolve, reject) => {
    request.onsuccess = function (event) {
      resolve(event.target.result);
    };
    request.onerror = function (event) {
      reject(event.target.error);
    };
  });
}

export async function getBookmarkById(id) {
  const db = await openDB();
  const transaction = db.transaction(['bookmarks'], 'readwrite');
  const store = transaction.objectStore('bookmarks');
  const request = store.get(id);
  return new Promise((resolve, reject) => {
    request.onsuccess = function (event) {
      resolve(event.target.result);
    };
    request.onerror = function (event) {
      reject(event.target.error);
    };
  });
}

export async function deleteBookmark(id) {
  const db = await openDB();
  const transaction = db.transaction(['bookmarks'], 'readwrite');
  const store = transaction.objectStore('bookmarks');
  const request = store.delete(id);
  return new Promise((resolve, reject) => {
    request.onsuccess = function (event) {
      resolve(event.target.result);
    };
    request.onerror = function (event) {
      reject(event.target.error);
    };
  });
}

export async function updateBookmark(bookmark) {
  const db = await openDB();
  const transaction = db.transaction(['bookmarks'], 'readwrite');
  const store = transaction.objectStore('bookmarks');
  const request = store.put(bookmark);
  return new Promise((resolve, reject) => {
    request.onsuccess = function (event) {
      resolve(event.target.result);
    };
    request.onerror = function (event) {
      reject(event.target.error);
    };
  });
}